package com.sihan.swvp.common;


import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
 * 非横表表头解析类
 * @author Administrator
 *
 */
public class HeaderParseTool 
{
	private int maxrow;
	private List<Header> hdList;
	private JSONArray jsa;
	private List<Header> leafs = new ArrayList<Header>();
	
	
	public HeaderParseTool(JSONArray jsa)
	{
		this.jsa = jsa;
		headerParse() ;
	}

	private List<Header> headerParse() 
	{
		maxrow = 1;
		hdList = new ArrayList<Header>();
		JSONArray columns = jsa;
		for (Object object : columns) 
		{
			JSONObject column = (JSONObject) object;
			if(!column.containsKey("hidden") || (column.containsKey("hidden") && !(Boolean)column.get("hidden")))
			{
				Header header = columnToHead(column,1);
				hdList.add(header);
			}
		}
		
		int col = 0;
		for (Header hd : hdList) {
			col += initPostion(hd,col,0);
		}
		return hdList;
	}
	
	
	private int initPostion(Header header,int col,int row)
	{
		header.setCol(col);
		header.setRow(row);
		header.setColSpan(getHeadMaxColNum(header));
		if(!header.isLeaf()){
			int headerMaxRow = getHeadMaxRowNum(header);
			if(row == 0 && headerMaxRow < maxrow)
			{
				row = maxrow - headerMaxRow;
				header.setRowSpan(row+1);
			}
			else{
				header.setRowSpan(maxrow - headerMaxRow + 1);
			}
			int subcol = col;
			for (Header head2 : header.getSubHeader()) 
			{
				subcol += initPostion(head2,subcol,row+1);
			}
		}
		else{
			header.setRowSpan(maxrow - row);
		}
		
		
		
		return header.getColSpan();
	}
	
//	public int getMaxRow(){
//		int temp = 0;
//		for (Header header : hdList) {
//			int row = getHeadMaxRowNum(header);
//			temp = temp > row ? temp : row;
//		}
//		
//		return temp;
//	}
	
	//最大行数
	private int getHeadMaxRowNum(Header head)
	{
		int flag = 0;
		
		List<Header> childsList = head.getSubHeader();
		if(head.isLeaf())
		{
			flag = flag > head.getTier() ? flag : head.getTier();
		}
		else
		{
			for (Header head2 : childsList) 
			{
				int fg = getHeadMaxRowNum(head2);
				flag = fg > flag ? fg : flag;
			}
		}
		
		return flag;
	}
	
	//最大列数
	private int getHeadMaxColNum(Header header)
	{
		int flag = 0;
		List<Header> childsList = header.getSubHeader();
		if(header.isLeaf())
		{
			flag += 1;
		}
		else
		{
			for (Header head2 : childsList) 
			{
				flag += getHeadMaxColNum(head2);
			}
		}
		
		return flag;
	}
	

	/**
	 * 
	 * @param column
	 * @param xy
	 * @return
	 */
	private Header columnToHead(JSONObject column,int tier)
	{
		Header header = new Header();
		header.setTier(tier);
		header.setText(column.getString("text"));
		header.setDataIndex(column.containsKey("dataIndex") ? column.getString("dataIndex") : "");
		header.setDataType(column.containsKey("dataType") ? column.getString("dataType") : "");
		header.setViewPosition(column.containsKey("viewposition") ? column.getString("viewposition") : "");
		header.setPrecision(column.containsKey("precision") ? CommonUtil.o2int(column.getString("precision")) : null);
		if(column.containsKey("columns")/* && ((JSONArray)column.get("columns")).size() != 0*/)
		{
			header.setLeaf(false);
			JSONArray jsArray = JSON.parseArray(column.getString("columns"));
			for (Object temp : jsArray) 
			{
				JSONObject column2 = JSON.parseObject(JSON.toJSONString(temp));
				if(!column2.containsKey("hidden") || (column2.containsKey("hidden") && !(Boolean)column2.get("hidden"))){
					header.addSubHeader(columnToHead(column2,tier+1));
					maxrow = maxrow > tier+1 ? maxrow : tier+1;
				}
			}
			
			header.setColSpan(1);
			header.setRowSpan(1);
		}
		else
		{
			header.setLeaf(true);
			header.setColSpan(1);
			header.setRowSpan(1);
			leafs.add(header);
		}
		
		return header;
	}

	public int getMaxrow() {
		return maxrow;
	}

	public void setMaxrow(int maxrow) {
		this.maxrow = maxrow;
	}

	public List<Header> getHdList() {
		return hdList;
	}

	public void setHdList(List<Header> hdList) {
		this.hdList = hdList;
	}

	public JSONArray getJsa() {
		return jsa;
	}

	public void setJsa(JSONArray jsa) {
		this.jsa = jsa;
	}

	public List<Header> getLeafs() {
		return leafs;
	}

	public void setLeafs(List<Header> leafs) {
		this.leafs = leafs;
	} 

	
	
	
	
}
